import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入页面组件
// import Login from '../views/Login.vue'
// import Home from '../views/Home.vue'
// import Users from '../views/Users.vue'
// import Right from '../views/Right/Right.vue'
// import Role from '../views/Right/Role.vue'
// import Categories from '../views/Good/Categories.vue'
// import Params from '../views/Good/Params.vue'
// import Goods from '../views/Good/Goods.vue'
// import GoodsAdd from '../views/Good/GoodsAdd.vue'
// import Orders from '../views/Orders.vue'
// import Reports from '../views/Reports.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/users',
    component: () => import('../views/Home.vue'),
    // home页面路由嵌套
    children: [
      {
        path: '/users',
        component: () => import('../views/Users.vue')
      },
      {
        path: '/rights',
        component: () => import('../views/Right/Right.vue')
      },
      {
        path: '/roles',
        component: () => import('../views/Right/Role.vue')
      },
      {
        path: '/categories',
        component: () => import('../views/Good/Categories.vue')
      },
      {
        path: '/params',
        component: () => import('../views/Good/Params.vue')
      },
      {
        path: '/goods',
        component: () => import('../views/Good/Goods.vue')
      },
      {
        path: '/goods/add',
        component: () => import('../views/Good/GoodsAdd.vue'),
        name: 'GoodsAdd'
      },
      {
        path: '/orders',
        component: () => import('../views/Orders.vue'),
        name: 'Orders'
      },
      {
        path: '/reports',
        component: () => import('../views/Reports.vue'),
        name: 'Reports'
      },
    ]
  },

]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // includes,判断值在不在数组中,返回一个布尔值
  if (['/', '/login'].includes(to.path)) {
    next()
  } else {
    if (sessionStorage.getItem('userInfo')) {
      next()
    } else {
      next('/login')
    }
  }

})

export default router
